package com.ruoyi.project.dict.spider.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.material.domain.Material;
import com.ruoyi.project.dict.spider.domain.MaterialSpider;
import com.ruoyi.project.dict.spider.service.IMaterialSpiderService;
import com.ruoyi.project.dict.tag.domain.MaterialTag;
import com.ruoyi.project.dict.tag.service.IMaterialTagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 资料爬取Controller
 *
 * @author shixiangwen
 * @date 2020-05-12
 */
@Controller
@RequestMapping("/dict/spider")
public class MaterialSpiderController extends BaseController {
    private String prefix = "dict/spider";

    @Autowired
    private IMaterialSpiderService materialSpiderService;

    @Autowired
    private IMaterialTagService materialTagService;

    @RequiresPermissions("dict:spider:view")
    @GetMapping()
    public String spider() {
        return prefix + "/spider";
    }

    /**
     * 查询资料爬取树列表
     */
    @RequiresPermissions("dict:spider:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialSpider materialSpider) {
        startPage();
        //materialSpider.setStatus(0);
        List<MaterialSpider> list = materialSpiderService.selectMaterialSpiderList(materialSpider);
        return getDataTable(list);
    }

    /**
     * 导出资料爬取列表
     */
    @RequiresPermissions("dict:spider:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialSpider materialSpider) {
        List<MaterialSpider> list = materialSpiderService.selectMaterialSpiderList(materialSpider);
        ExcelUtil<MaterialSpider> util = new ExcelUtil<MaterialSpider>(MaterialSpider.class);
        return util.exportExcel(list, "spider");
    }

    /**
     * 新增资料爬取
     */
    @GetMapping(value = {"/add/{id}", "/add/"})
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("materialSpider", materialSpiderService.selectMaterialSpiderById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存资料爬取
     */
    @RequiresPermissions("dict:spider:add")
    @Log(title = "资料爬取", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialSpider materialSpider) {
        return toAjax(materialSpiderService.insertMaterialSpider(materialSpider));
    }

    /**
     * 修改资料爬取
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MaterialSpider materialSpider = materialSpiderService.selectMaterialSpiderById(id);
        mmap.put("materialSpider", materialSpider);
        return prefix + "/edit";
    }

    /**
     * 导入到学习资料
     *
     * @param ids
     * @param mmap
     * @return
     */
    @GetMapping("/material/{ids}")
    public String importToMaterial(@PathVariable("ids") String ids, ModelMap mmap) {

        mmap.put("ids", ids);
        List<MaterialTag> tags = materialTagService.selectMaterialTagList(new MaterialTag());

        Long[] idList = Convert.toLongArray(ids);
        String title = "";
        if (idList.length == 1) {
            MaterialSpider spider = materialSpiderService.selectMaterialSpiderById(idList[0]);
            if (spider != null) {
                title = spider.getTitle();
            }
        }
        mmap.put("title", title);

        mmap.put("tags", tags);

        return prefix + "/material";
    }

    /**
     * 导入到现有学习资料
     *
     * @param ids
     * @param mmap
     * @return
     */
    @GetMapping("/materialOld/{ids}")
    public String importToOldMaterial(@PathVariable("ids") String ids, ModelMap mmap) {

        return prefix + "/materialOld";
    }

    @RequiresPermissions("dict:spider:add")
    @Log(title = "资料爬取", businessType = BusinessType.INSERT)
    @PostMapping("/materialOld")
    @ResponseBody
    public AjaxResult materialOldSave(Integer materialId, String ids) {
        return toAjax(materialSpiderService.materialSave(materialId, Convert.toLongArray(ids)));
    }

    @RequiresPermissions("dict:spider:add")
    @Log(title = "资料爬取", businessType = BusinessType.INSERT)
    @PostMapping("/material")
    @ResponseBody
    public AjaxResult materialSave(Material material, String ids) {
        return toAjax(materialSpiderService.materialSave(material, Convert.toLongArray(ids)));
    }

    /**
     * 修改保存资料爬取
     */
    @RequiresPermissions("dict:spider:edit")
    @Log(title = "资料爬取", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialSpider materialSpider) {
        return toAjax(materialSpiderService.updateMaterialSpider(materialSpider));
    }

    /**
     * 更新排序
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("dict:spider:edit")
    @Log(title = "资料爬取", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSeq")
    @ResponseBody
    public AjaxResult updateSeq(Long pid, String ids) {
        return toAjax(materialSpiderService.updateSeq(pid, Convert.toLongArray(ids)));
    }

    /**
     * 删除
     */
    @RequiresPermissions("dict:spider:remove")
    @Log(title = "资料爬取", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(materialSpiderService.deleteMaterialSpiderById(id));
    }

    /**
     * 选择资料爬取树
     */
    @GetMapping(value = {"/selectSpiderTree/{id}", "/selectSpiderTree/"})
    public String selectSpiderTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("materialSpider", materialSpiderService.selectMaterialSpiderById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载资料爬取树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = materialSpiderService.selectMaterialSpiderTree();
        return ztrees;
    }
}
